home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 36 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  127 lines

  1. Path: gryphon.phoenix.net!usenet
  2. From: brucew@phoenix.net (Bruce Wedding)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Count lines in file?
  5. Date: Mon, 01 Jan 1996 06:40:34 GMT
  6. Organization: BranPaul Systems
  7. Message-ID: <4c7t8p$p5l@gryphon.phoenix.net>
  8. References: <4bfnqu$btj@news1.netzone.com> <4bi4nr$3qe@castle.nando.net> <4bk30e$l08@news1.netzone.com> <Pine.A32.3.91.951224222853.23094E@red.weeg.uiowa.edu> <4bm9tk$h5a@crl14.crl.com> <820334651snz@genesis.demon.co.uk> <4c4v8b$kb6@crl5.crl.com>
  9. NNTP-Posting-Host: dial22.phoenix.net
  10. X-Newsreader: Moe's Newsreader    
  11.  
  12. kossick@crl.com (Paul J. Kossick) wrote:
  13.  
  14.  
  15. >That's an interesting point I thought of when talking to someone else 
  16. >about this via E-Mail...I would think that fgets would be SLOWER than 
  17. >getc, since it actually has to execute code similar to getc to fill the 
  18. >buffer in the first place...as well as other work such as checking to see 
  19. >if the buffer is full.  Since you don't NEED to retain the lines intact 
  20. >in order to count them, a buffer is basicly surplusage.
  21. >
  22. >Oh, before someone else points it out:  One problem with getc might be if 
  23. >the last line of the program doesn't have a terminating newline 
  24. >character, i.e. it terminates with the eof.  This could cause the count 
  25. >to be off by one in such cases.  fgets avoids this by counting the line 
  26. >anyways (though there's still that full-buffer problem), and I suppose 
  27. >some code could be devised to make getc deal with it.
  28.  
  29. You were right about this.  I just let it go though.
  30.  
  31. >Anyone want to run these as an experiment, and report the results to the 
  32. >group?  I'd do it, but why should I hog all the fun?  ;)
  33.  
  34. Here is the code I ran.  I've determined that the results are
  35. completely inconclusive and probably not only compiler dependant, but
  36. compiler switch dependant also.  I ran it on a 6000 line file,
  37. compiled with MSC 8.00c on a dos box.  With the default command line,
  38. the fgets was usually about 50-100 % faster.  With the optimizer set
  39. to "fastest code" ( /O2 for MSC), the getc() version was marginally
  40. faster or equal.  I'd say it really doesn't make a difference which to
  41. use.
  42.  
  43. Here is the code:
  44.  
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <time.h>
  48.  
  49. long int char_by_char( FILE *fp);
  50. long int line_by_line( FILE *fp);
  51.  
  52. long int char_by_char( FILE *fp)
  53. {
  54.    long int count = 0;
  55.    int c;
  56.  
  57.    while ((c = getc(fp)) != EOF)
  58.    {
  59.       if (c == '\n')
  60.         ++count;
  61.    }
  62.    return count;
  63. }
  64.  
  65.  
  66. long int line_by_line( FILE *fp)
  67. {
  68.    char buf[256];
  69.    long int count = 0;
  70.  
  71.    while ( fgets(buf, 256, fp))
  72.    {
  73.       ++count;
  74.    }
  75.    return count;
  76. }
  77.  
  78. int main (int argc, char *argv[])
  79. {
  80.       FILE *fp;
  81.  
  82.       time_t start = 0, end = 0;
  83.       long int nl = 0;
  84.  
  85.       fp = fopen(argv[1],"rb");
  86.       if (fp == NULL)
  87.       {
  88.             printf("Cannot open %s\n", argv[1]);
  89.             exit(1);
  90.       }
  91.  
  92.       start = clock();
  93.       nl = char_by_char( fp);
  94.       end = clock();
  95.       printf("Using getc(): ET: %ld \t %ld Lines\n", end - start, nl);
  96.       
  97.       nl = 0;
  98.       rewind(fp);
  99.       start = clock();
  100.       nl = line_by_line( fp );
  101.       end = clock();
  102.       printf("Using fgets(): ET: %ld \t %ld Lines\n", end - start,
  103. nl);
  104.  
  105.       nl = 0;
  106.       rewind(fp);
  107.       start = clock();
  108.       nl = char_by_char( fp);
  109.       end = clock();
  110.       printf("Using getc(): ET: %ld \t %ld Lines\n", end - start, nl);
  111.       
  112.       nl = 0;
  113.       rewind(fp);
  114.       start = clock();
  115.       nl = line_by_line( fp );
  116.       end = clock();
  117.       printf("Using fgets(): ET: %ld \t %ld Lines\n", end - start,
  118. nl);
  119.  
  120.       return 0;
  121. }
  122.  
  123. Bruce D. Wedding                        Have Compiler, Will Travel!
  124.               Perspicacious Progamming Performed Promptly
  125. Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
  126.  
  127.